<?php
class GeneralReportGroup extends ORDataObject {
	var $general_report_group_id	=	'';
  	var $name						=	'';
  	var $description				=	'';
  	var $owner						=	'';

  	var	$_table	=	'general_report_group';
  	var	$_key	=	'general_report_group_id';

  	const 	GROUP_ID_UNKNOWN	=	0;
	const	GROUP_NAME_UNKNOWN	=	"Unknown group";

	public function setup($groupId = -1){
		if($groupId == 0){
			$this->setupUnknownGroup();
		}

		else if($groupId > 0){
			$this->general_report_group_id	=	$groupId;
			parent::populate('general_report_group_id');
		}
	}

	public function setData($groupName, $groupDesc, $owner){
		$data	=	array(
			'name'			=>	$groupName,
			'description'	=>	$groupDesc,
			'owner'			=>	$owner
		);
		$this->populateArray($data);
  	}

  	public function getName(){
  		return $this->name;
  	}

  	public function getDescription(){
  		return $this->description;
  	}

  	public function getOwner(){
  		return $this->owner;
  	}

  	public function getGroupsByUserId($userId){
  		return array();// TODO: Rewrite
  	}

	public function getAllGroups($userId){
  		$ds		=	&new Datasource_sql();
  		$sql	=	array(
  			"cols"	=>	"$this->_key id, name",
  			"from"	=>	"$this->_table",
  			"where"	=>	"owner=$userId",
  			"orderby"	=>	"name"
  		);
  		$ds->setup($this->_db, $sql);
  		$groups	=	$ds->toArray('id', 'name');
		$res[self::GROUP_ID_UNKNOWN]	=	self::GROUP_NAME_UNKNOWN;
		foreach ($groups as $grpId => $grpName){
			$res[$grpId]	=	$grpName;
		}

  		return $res;
  	}

  	public function deleteGroup($groupId = ''){
  		$sql	=	"DELETE FROM $this->_table WHERE $this->_key=$groupId";
  		$this->_execute($sql);
  	}

  	public function setupUnknownGroup(){
		$this->populateArray(
			array(
				'general_report_group_id'	=> 	self::GROUP_ID_UNKNOWN,
				'name' 					 	=>	self::GROUP_NAME_UNKNOWN
			)
		);
  	}
}
?>